/**
 * Copyright © 2013 Creativor Studio.
 * About license information, please see LICENSE.txt.
 */
package org.creativor.rayson.share.transport.http;

import org.creativor.rayson.share.annotation.NotNull;
import org.creativor.rayson.share.transport.ByteArrayBuffer;

/**
 * Stands for an request line of {@link HttpRequest}.
 * 
 * @author creativor
 */
public class HttpRequestLine implements HttpStartLine {
	private final HttpMethod method;
	private final String uri;
	private final String version;

	public HttpRequestLine(@NotNull final HttpMethod method,
			@NotNull final String uri, @NotNull final String version) {
		this.method = method;
		this.uri = uri;
		this.version = version;
	}

	public HttpRequestLine(final String uri) {
		this(HttpConstants.REQUEST_METHOD, uri, HttpConstants.HTTP_VERSION);
	}

	/**
	 * @return the method
	 */
	public HttpMethod getMethod() {
		return method;
	}

	/**
	 * @return the uri
	 */
	public String getUri() {
		return uri;
	}

	/**
	 * @return the version
	 */
	public String getVersion() {
		return version;
	}

	@Override
	public String toString() {
		final StringBuffer sb = new StringBuffer();
		sb.append("{");
		sb.append("method:" + this.method);
		sb.append(", uri:" + this.uri);
		sb.append(", version:" + this.version);
		sb.append("}");
		return sb.toString();
	}

	@Override
	public void writeTo(final ByteArrayBuffer buffer) {
		buffer.write(getMethod().name().getBytes());
		buffer.write(HttpConstants.SPACE);
		buffer.write(getUri().getBytes());
		buffer.write(HttpConstants.SPACE);
		buffer.write(getVersion().getBytes());
		buffer.write(HttpConstants.CRLF);
	}
}
